<%--
   processVariable
   Copyright (c) 2014 ISPERP
   Version : 1.0.0
 --%>
<%@ tag language="java" pageEncoding="UTF-8"
	trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>


<%@ attribute name="id" required="true" rtexprvalue="true"
	type="java.lang.String" description="id"%>

<%@ attribute name="processKey" required="true" rtexprvalue="true"
	type="java.lang.String" description="processKey"%>

<%@ attribute name="targetControlId" required="false" rtexprvalue="true"
	type="java.lang.String" description="targetControlId"%>

<%@ attribute name="targetIsEditor" required="false" rtexprvalue="true"
	type="java.lang.Boolean" description="targetControlId"%>

<%@ attribute name="useAsScript" required="false" rtexprvalue="true"
	type="java.lang.Boolean" description="targetControlId"%>

<div>
	<a class="nui-txt-link"
		href="javascript:$('#${id}processVariableDialog').dialog('open');">插入流程变量
	</a>
</div>

<div id="${id}processVariableDialog" title="插入流程变量">
	<div id="${id}processVariableTab" class="box tabs">
		<div class="header">
			<ul>
				<li><a class="current" href="#${id}ProcessVariable">流程变量</a></li>
				<c:if test="${useAsScript ne true }">
				<li><a href="#${id}ProcessTaskExecutor">流程执行者</a></li></c:if>
				<li><a href="#${id}NextProcessTaskExecutor">下一步候选人</a></li>
				<li><a href="#${id}ProcessEntityField">业务表字段</a></li>
			</ul>
			<h2>&nbsp;</h2>
		</div>
		<div style="padding:5px 15px" class="content tabbed">
			<div id="${id}ProcessVariable">

				<ul class="">
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmStarter');">流程发起人帐号</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('businessKey');">业务表主键</a>
					</li>
				</ul>

			</div>
			<div id="${id}ProcessTaskExecutor">
				<ul>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmCurrentUserBean.name');">帐号</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmCurrentUserBean.fullname');">姓名</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmCurrentUserBean.position');">职位</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmCurrentUserBean.email');">邮箱</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmCurrentUserBean.mobile');">移动电话</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmCurrentUserBean.department');">所在部门</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmCurrentUserBean.uType');">用户类型</a>
					</li>

				</ul>
			</div>
			<c:if test="${useAsScript ne true }">
			<div id="${id}NextProcessTaskExecutor">
				<ul>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmNextUserBean.name');">帐号</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmNextUserBean.fullname');">姓名</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmNextUserBean.position');">职位</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmNextUserBean.email');">邮箱</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmNextUserBean.mobile');">移动电话</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmNextUserBean.department');">所在部门</a>
					</li>
					<li><a class="nui-txt-link"
						href="javascript:${id}InsertToTargetControl('bpmNextUserBean.uType');">用户类型</a>
					</li>

				</ul>
			</div></c:if>
			<div id="${id}ProcessEntityField"></div>

		</div>
	</div>
</div>
<script type="text/javascript">
	$("#${id}processVariableTab").tabs();
	$("#${id}processVariableDialog").dialog({
		autoOpen : false,
		width : 380,
		height : 400,

		buttons : {"关闭" : function() {$(this).dialog("close");}}
		});
	
	$.getJSON("bpm/processSetting/getDbEntityColumnByProcDefKey?processKey=${processKey}",function(data){
		var html="<ul style='overflow:auto;height:290px'>";
	
		$(data).each(function(index,item){
			html+="<li><a class='nui-txt-link' href='javascript:${id}InsertToTargetControl(\"bpmBusinessModel."+item.column_name+"\");'>"+item.name+"</a></li>";
			
		});
	
		html+="</ul>";
		$("#${id}ProcessEntityField").html(html);
	});
	
</script>

<c:choose>
	<c:when test="${targetIsEditor eq true }">

		<script type="text/javascript">
			function ${id}InsertToTargetControl(txt) {
				txt= txt+" ";
				${targetControlId}.replaceSelection(txt, focus);
				${targetControlId}.focus();
			}
		</script>
	</c:when>
	<c:otherwise>
		<c:set var="varTxt"
			value="${useAsScript eq true?'txt=txt;':'txt=\"$\"+\"{\"+txt+\"}\";'}">
		</c:set>

		<script type="text/javascript">
			function ${id}InsertToTargetControl(txt) {
				var myField = document.getElementById("${targetControlId}");
				${varTxt }
				//txt="$"+"{"+txt+"}";
				//IE support
				if (document.selection) {
					myField.focus();
					sel = document.selection.createRange();
					sel.text = txt;
					sel.select();
				}
				//MOZILLA/NETSCAPE support
				else if (myField.selectionStart || myField.selectionStart == '0') {
					var startPos = myField.selectionStart;
					var endPos = myField.selectionEnd;
					// save scrollTop before insert www.jbxue.com
					var restoreTop = myField.scrollTop;
					myField.value = myField.value.substring(0, startPos) + txt + myField.value.substring(endPos, myField.value.length);
					if (restoreTop > 0) {
						myField.scrollTop = restoreTop;
					}
					myField.focus();
					myField.selectionStart = startPos + txt.length;
					myField.selectionEnd = startPos + txt.length;
				} else {
					myField.value += txt;
					myField.focus();
				}

			}
		</script>

	</c:otherwise>
</c:choose>
